package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 查询产品参数
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    @ResponseBody
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid") Long cid){

        List<SpecGroup> groups = this.specificationService.queryGroupsByCid(cid);

        if(CollectionUtils.isEmpty(groups)){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(groups);
    }

    /**
     * 新增产品分组
     * @param specGroup
     * @return
     */
    @PostMapping("group")
    public ResponseEntity insertGroups(@RequestBody SpecGroup specGroup){

        this.specificationService.insertGroup(specGroup);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改产品分组
     * @param specGroup
     * @return
     */
    @PutMapping("group")
    public ResponseEntity updateGroup(@RequestBody SpecGroup specGroup){

        this.specificationService.updateGroup(specGroup);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除产品分组
     * @param id
     * @return
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity deleteGroup(@PathVariable("id") Integer id){

        this.specificationService.deleteGroup(id);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 根据id查询产品规格参数
     * //http://api.leyou.com/api/item/spec/params?cid=76
     * @param gid
     * @return
     */
    @RequestMapping("params")
    public ResponseEntity<List<SpecParam>> queryGroupsByGid(@RequestParam(value = "gid", required = false) Long gid,
                                                            @RequestParam(value = "cid", required = false) Long cid,
                                                            @RequestParam(value = "generic", required = false) Boolean generic,
                                                            @RequestParam(value = "searching", required = false) Boolean searching){



        List<SpecParam> params = this.specificationService.queryGroupsByGid(gid,cid,generic,searching);

        if(CollectionUtils.isEmpty(params)){

            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(params);
    }

    /**
     * 新增产品规格参数
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public ResponseEntity insertParams(@RequestBody SpecParam specParam){

        specificationService.insertParams(specParam);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改产品规格参数
     * @param specParam
     * @return
     */
    @PutMapping("param")
    public ResponseEntity updateParams(@RequestBody SpecParam specParam){

        specificationService.updateParams(specParam);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除产品规格参数
     * @param id
     * @return
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity deleteParams(@PathVariable Integer id){

        specificationService.deleteParams(id);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询组及组下规格参数
     * @param cid
     * @return
     */
    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> list = this.specificationService.querySpecsByCid(cid);
        if(list == null || list.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

    @GetMapping("group/param/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsWithParams(@PathVariable("cid") Long cid){
        List<SpecGroup> groups = specificationService.queryGroupsWithParams(cid);

        if(CollectionUtils.isEmpty(groups)){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(groups);
    }




}
